package com.gaga.console.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gaga.common.enums.MenuTypeEnum;
import com.gaga.console.base.service.impl.SuperServiceImpl;
import com.gaga.console.core.entity.SysMenu;
import com.gaga.console.core.entity.SysRoleMenu;
import com.gaga.console.core.mapper.SysRoleMenuMapper;
import com.gaga.console.core.service.SysMenuService;
import com.gaga.console.core.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色权限表 服务实现类
 * </p>
 *
 * @author liujia
 * @since 2022-07-14
 */
@Service
public class SysRoleMenuServiceImpl extends SuperServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {
    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public List<String> getMenuIdByRoleId(Long roleId) {
        LambdaQueryWrapper<SysRoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoleMenu::getRoleId, roleId);

        //1.根据角色ID，查询菜单列表
        List<SysRoleMenu> sysRolePermissions = this.baseMapper.selectList(lambdaQueryWrapper);
        //2.将menuId转换为List
        List<Long> menuIds = sysRolePermissions.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        //3.根据menuId查询所有的满足条件的菜单列表,其中type=2为按钮
        List<SysMenu> sysMenuList = sysMenuService.list(new LambdaQueryWrapper<SysMenu>()
                .eq(SysMenu::getType, MenuTypeEnum.BUTTON.getCode()).in(SysMenu::getId, menuIds));
        return sysMenuList.stream().map(SysMenu::getPermission).collect(Collectors.toList());
    }
}
